home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 370 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  118 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: start array at k, not 0
  5. Date: Thu, 04 Jan 96 20:31:26 GMT
  6. Organization: none
  7. Message-ID: <820787486snz@genesis.demon.co.uk>
  8. References: <Pine.OSF.3.91.960104095358.22268B-100000@io.UWinnipeg.ca>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <Pine.OSF.3.91.960104095358.22268B-100000@io.UWinnipeg.ca>
  15.            wsimpson@uwinnipeg.ca "Bill Simpson" writes:
  16.  
  17. >I have come up with the following 2 methods that allow one to talk about
  18. >an array that starts at element k rather than 0.  E.g. 10 element array
  19. >y[k] to y[k+10].  Both seem to work.  Is this illusory?  Is one of the
  20. >2 ways better (or a way I haven't mentioned)?
  21. >
  22. >These programs set up y[5] to y[14].
  23. >
  24. >Thanks very much for any comments.
  25. >
  26. >Bill Simpson
  27. >
  28. >/*method 1*/
  29. >#include <stdio.h>
  30. >
  31. >int main(void)
  32. > {
  33. > int i, x[10];
  34. > int* y;
  35. > int offset=5;  /*ie 1st index at 5, y[5]-y[14] */
  36. > y=x-offset;
  37.  
  38. It is illegal for a pointer to point outside the bounds of an object (except
  39. the special case of one past the end) hence the expression x-offset is
  40. illegal here. The only values of offset where this would be legal are 0
  41. through -10.
  42.  
  43. ...
  44.  
  45. >/*method 2*/
  46. >#include <stdio.h>
  47. >#include <stdlib.h>
  48. >
  49. >int main(void)
  50. > {
  51. > int i;
  52. > int* y;
  53. > int offset=5;  
  54. > int n=10;       
  55. > y=malloc(n*sizeof(int));
  56.  
  57. Were you trying to modify y by the offset here - as it stands it simply
  58. points at the start of the malloc'd area. Later you access up to y[14] which
  59. is beyond the end of the array and hence is illegal.
  60.  
  61. The rules are the same for a malloc'd object as any other - you can't can't
  62. ue pointer arithmetic to create a pointer beyond its bounds (except one
  63. past the end).
  64.  
  65. Therefore when approaching a problem like this you must perform any offset
  66. adjustments in the indices rather than the base pointers. So you could
  67. use:
  68.  
  69. #include <stdio.h>
  70.  
  71. int main(void)
  72.  {
  73.  int i, x[10];
  74.  int* y;
  75.  int offset=5;  /*ie 1st index at 5, y[5]-y[14] */
  76.  
  77.  printf("offset=%d\n",offset);
  78.  
  79.  for (i=offset;i<10+offset;i++)
  80.         {
  81.         y[i-offset]=i;
  82.         printf("%d\n",y[i-offset]);
  83.         }
  84.  return 0;
  85.  }
  86.  
  87. Or you could look at it a different way and write:
  88.  
  89. #include <stdio.h>
  90.  
  91. int main(void)
  92.  {
  93.  int i, x[10];
  94.  int* y;
  95.  int offset=5;  /*ie 1st index at 5, y[5]-y[14] */
  96.  
  97.  printf("offset=%d\n",offset);
  98.  
  99.  for (i=0;i<10;i++)
  100.         {
  101.         y[i]=i+offset;
  102.         printf("%d\n",y[i]);
  103.         }
  104.  return 0;
  105.  }
  106.  
  107. which is perhaps the most common 'C' way (y isn't really needed in either
  108. of these). This sort of problem mostly comes up when porting from other
  109. languages. With some rare exceptions when you are designing a program with
  110. C in mind from the start the problem can be avoided (e.g. as I've done in
  111. the last example).
  112.  
  113. -- 
  114. -----------------------------------------
  115. Lawrence Kirby | fred@genesis.demon.co.uk
  116. Wilts, England | 70734.126@compuserve.com
  117. -----------------------------------------
  118.